package com.android.tea.shopper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.tea.R;
import com.android.tea.TeaEncyclopediaApp;
import com.android.tea.api.ApiResult;
import com.android.tea.api.ApiReturnResultListener;
import com.android.tea.api.TeaApi;
import com.android.tea.entity.Clientindustry;
import com.android.tea.util.TeaLog;

public class ShopperClassifyActivity extends Activity {

	private ListView listView;
	private TeaClass teaClassAdpater;
	ProgressDialog myDialog;
	TeaApi userApi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.allfriends);
		TeaEncyclopediaApp.list.add(this);
		
		listView = (ListView) findViewById(R.id.listView1);
		listView.setCacheColorHint(0);

	}
	

	@Override
	protected void onResume() {
		initAllList();
		super.onResume();
	}

	public Stack<String> stack = new Stack<String>();
	HashMap<String, Clientindustry> map = null;
	Clientindustry clientindustry = null;
	ApiReturnResultListener apiReturnResultListener = new ApiReturnResultListener() {

		@Override
		public <T> void onReturnSucceedResult(int requestCode,
				ApiResult<T> apiResult) {
			TeaLog.e("aaaa", "onReturnSucceedResult");
			clientindustry = (Clientindustry) apiResult.getEntity();

			downList(" 0 ");
			myDialog.dismiss();
		}

		@Override
		public <T> void onReturnFailResult(int requestCode,
				ApiResult<T> apiResult) {
			TeaLog.e("aaaa", "onReturnFailResult");
			clientindustry = null;
			Toast.makeText(ShopperActivity.instance, "获取分类信息失败",
					Toast.LENGTH_LONG).show();
			myDialog.dismiss();
		}
	};

	private boolean initComponents(String key) {

		ArrayList<Clientindustry> temArrayList = clientindustry
				.getArrayList(key);
		int countNum = temArrayList.size();
		if (countNum > 0) {
			teaClassAdpater = new TeaClass(temArrayList);
			listView.setAdapter(teaClassAdpater);
			return true;
		} else {
			// Toast.makeText(ShopperActivity.instance, "没有下级分类",
			// Toast.LENGTH_LONG).show();
			return false;
		}

	}

	@Override
	public void onBackPressed() {
		upList();
		// super.onBackPressed();
//		Activity parent = this.getParent();
//		if(parent!=null) {
//			parent.onBackPressed();
//		}else{
//			super.onBackPressed();
//		}
	}
	
	public void upList() {
		if (stack.size() > 2) {
			String listKey = stack.pop();
			TeaLog.e("取出", listKey);
			initComponents(stack.peek());

		}
	}

	public void downList(String key) {

		if (initComponents(key)) {
			TeaLog.e("放入", key);
			stack.push(key);
		}

	}

	private void initAllList() {
		if (clientindustry == null) {
			myDialog = ProgressDialog.show(ShopperActivity.instance, "请稍候",
					"正在获取商家分类信息", true);
			myDialog.setCancelable(true);
			myDialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {

				}
			});
			userApi = new TeaApi(this);
			userApi.setReturnResultListener(apiReturnResultListener);
			userApi.getTeaSort(1);
		} else {
			downList(" 0 ");
		}
	}

	class TeaClass extends BaseAdapter {
		ArrayList<Clientindustry> dateArrayList = new ArrayList<Clientindustry>();

		public TeaClass(ArrayList<Clientindustry> dateArrayList) {
			this.dateArrayList = dateArrayList;
		}

		@Override
		public int getCount() {
			return dateArrayList.size();
		}

		@Override
		public Clientindustry getItem(int position) {
			return dateArrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tv = new TextView(ShopperActivity.instance);
			final Clientindustry teClientindustry = getItem(position);
			tv.setText(teClientindustry.getCoinName());
			tv.setTextColor(Color.BLACK);
			tv.setPadding(10, 5, 10, 5);
			tv.setMinWidth(30);
			tv.setTextSize(16.f);
			tv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					String getString = teClientindustry.getCoinId();
					downList(getString);
				}
			});
			return tv;
		}
	}
}
